<template>
  <div class="login-container">
    <el-card class="login-card">
      <template #header>
        <div class="card-header">
          <h2>学生选课系统</h2>
          <p>请登录您的账户</p>
        </div>
      </template>
      
      <el-form :model="loginForm" :rules="rules" ref="loginFormRef" label-width="0px">
        <el-form-item prop="username">
          <el-input 
            v-model="loginForm.username" 
            placeholder="请输入用户名"
            size="large"
            prefix-icon="User"
          />
        </el-form-item>
        
        <el-form-item prop="password">
          <el-input 
            type="password" 
            v-model="loginForm.password" 
            placeholder="请输入密码"
            size="large"
            prefix-icon="Lock"
            show-password
            @keyup.enter="handleLogin"
          />
        </el-form-item>
        
        <el-form-item>
          <el-button 
            type="primary" 
            size="large"
            @click="handleLogin" 
            :loading="loading"
            style="width: 100%"
          >
            {{ loading ? '登录中...' : '登 录' }}
          </el-button>
        </el-form-item>
      </el-form>
      
      <div class="demo-accounts">
        <h4>测试账户：</h4>
        <p><strong>管理员：</strong> admin / admin123</p>
        <p><strong>教师：</strong> teacher1 / teacher123</p>
        <p><strong>学生：</strong> student1 / student123</p>
      </div>
    </el-card>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { ElMessage } from 'element-plus'
import { useAuthStore } from '@/stores/auth'

const loginFormRef = ref(null)
const loginForm = reactive({
  username: '',
  password: ''
})
const loading = ref(false)
const router = useRouter()
const route = useRoute()
const authStore = useAuthStore()

const rules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, message: '密码长度至少为 6 位', trigger: 'blur' },
  ]
}

const handleLogin = () => {
  loginFormRef.value.validate(async (valid) => {
    if (valid) {
      loading.value = true
      try {
        const success = await authStore.login(loginForm.username, loginForm.password)
        if (success) {
          ElMessage.success('登录成功！')
          // 登录成功后跳转到之前尝试访问的页面，或者默认首页
          const redirect = route.query.redirect || '/'
          await router.push(redirect)
        } else {
          ElMessage.error('用户名或密码错误')
        }
      } catch (error) {
        console.error('登录异常:', error)
        ElMessage.error('登录失败，请重试')
      } finally {
        loading.value = false
      }
    }
  })
}
</script>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  padding: 20px;
}

.login-card {
  width: 100%;
  max-width: 400px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
  border-radius: 12px;
  border: none;
}

.card-header {
  text-align: center;
  margin-bottom: 20px;
}

.card-header h2 {
  color: #303133;
  margin: 0 0 8px 0;
  font-weight: 600;
}

.card-header p {
  color: #909399;
  margin: 0;
  font-size: 14px;
}

.demo-accounts {
  margin-top: 20px;
  padding: 15px;
  background-color: #f8f9fa;
  border-radius: 6px;
  font-size: 12px;
  color: #666;
}

.demo-accounts h4 {
  margin: 0 0 8px 0;
  color: #333;
  font-size: 13px;
}

.demo-accounts p {
  margin: 4px 0;
  line-height: 1.4;
}

:deep(.el-card__header) {
  padding: 20px 20px 0 20px;
}

:deep(.el-card__body) {
  padding: 0 20px 20px 20px;
}
</style> 